﻿using SocketServer.Model;
using System;
using System.Collections.Generic;
namespace SocketServer.WebSocketAction
{
    /// <summary>
    /// 程序启动时加载的interface
    /// </summary>
    /// <remarks>
    /// 继承该接口的所有子类首次创建在WebSocketServerStartHelper的LoadAction方法中
    /// </remarks>
    public interface IWebSocketServerAction : ICloneable
    {
        /// <summary>
        /// 非泛型的公共方法
        /// </summary>
        List<MethodHelper> MethodList { get; set; }

        /// <summary>
        /// 过滤器
        /// </summary>
        List<SocketAttribute.WebSocketFilterAttribute> FilterList { get; set; }

        /// <summary>
        /// 当前请求的socket链接
        /// </summary>
        /// <remarks>
        /// 当请求到达方法里面时会产生
        /// </remarks>
        Fleck.IWebSocketConnection Socket { get; set; }

        /// <summary>
        /// 缓存的SocketList最大数量(默认值100)
        /// </summary>
        int CacheMaxCount { get; set; }

        /// <summary>
        /// 缓存在当前实体中的Socket
        /// </summary>
        List<Fleck.IWebSocketConnection> SocketList { get; set; }

        /// <summary>
        /// 客户端请求的相关参数
        /// </summary>
        SocketRequest Request { get; set; }

        /// <summary>
        /// 是否结束请求
        /// </summary>
        bool IsEnd { get; set; }

        /// <summary>
        /// 方法返回值
        /// </summary>
        object Result { get; set; }

        /// <summary>
        /// 异常信息
        /// </summary>
        Exception ExceptionResult { get; set; }

        /// <summary>
        /// 对这个类中缓存的客户端广播消息
        /// </summary>
        /// <param name="Bol">是否成功</param>
        /// <param name="Status">状态</param>
        /// <param name="Message">消息</param>
        /// <param name="Data">数据</param>
        /// <returns>是否完成了所有的广播,一般情况下失败是因为在广播时有客户端缓存或断开了</returns>
        bool BroadcastMessage(bool Bol, int Status, string Message, object Data);

        /// <summary>
        /// 对这个类中缓存的客户端广播消息
        /// </summary>
        /// <param name="responseResult">广播内容</param>
        /// <returns>是否完成了所有的广播,一般情况下失败是因为在广播时有客户端缓存或断开了</returns>
        bool BroadcastMessage(PublicEntity.ResponseResult responseResult);

        /// <summary>
        /// 缓存加入到集合之前的验证
        /// </summary>
        /// <param name="Request">请求的参数信息</param>
        /// <returns>是否验证成功</returns>
        bool CacheBeforeValidate(SocketRequest Request);

        /// <summary>
        /// 缓存清理前执行
        /// </summary>
        /// <param name="ClassName">请求到的类型名</param>
        void CacheClearBefore(string ClassName);

        /// <summary>
        /// 缓存清理后执行
        /// </summary>
        /// <param name="ClassName">请求到的类型名</param>
        void CacheClearAfter(string ClassName);

        /// <summary>
        /// 客户端关闭链接后执行
        /// </summary>
        /// <param name="Client">客户端</param>
        void CloseAfter(Fleck.IWebSocketConnection Client);

        /// <summary>
        /// 清除不在线的缓存
        /// </summary>
        /// <param name="ClientAction">遇到在线缓存时触发的委托</param>
        void ClearNotOnlineCache(Action<Fleck.IWebSocketConnection> ClientAction = null);
    }
}
